// 转换为树形结构
function buildYearlyTree(stats) {
  const yearMap = new Map()

  stats.forEach(({ year, month, post_count }) => {
    if (!yearMap.has(year)) {
      yearMap.set(year, {
        year,
        total: 0,
        months: new Map(),
      })
    }

    const yearData = yearMap.get(year)
    yearData.total += post_count
    yearData.months.set(month, post_count)
  })

  return Array.from(yearMap.values())
    .sort((a, b) => b.year - a.year)
    .map((yearData) => ({
      title: `${yearData.year}年`,
      key: String(yearData.year),
      count: yearData.total,
      children: Array.from(yearData.months.entries())
        .sort(([a], [b]) => b - a)
        .map(([month, count]) => ({
          title: `${month}月(${count}篇)`,
          key: `${yearData.year}-${month.toString().padStart(2, '0')}`,
          isLeaf: true,
        })),
    }))
}
module.exports = {
  buildYearlyTree,
}
